#include<iostream>
using namespace std;
#include<string>
// push_back() 插入字符
void test_string1()
{
	string s;
	s.push_back('C');
	s.push_back('S');
	s.push_back('D');
	s.push_back('N');
	cout << s << endl;
	cout << s.capacity() << endl;
	cout << s.size() << endl << endl;

	string s1("hello ");
	s1.push_back('W');
	cout << s1 << endl;
}
// append()插入字符串
void test_string2()
{
	string s1("I ");
	string s2("like");

	// append(string)完成两个string类的对象拼接;
	s1.append(s2);
	cout << s1 << endl; // I like
	
	// append(str)在字符串后面追加字符串" C++";
	s1.append(" C++");
	cout << s1 << endl; // I like C++

	// append(n,char) 将n个字符char 追加到字符串string类对象中
	s1.append(3, '!');
	cout << s1 << endl;// I like C++!!!
}
void test_string3()
{
	string s1("hello");
	s1 += ' ';
	cout << s1 << endl;// hello		

	s1 += 'C';
	cout << s1 << endl; // hello C

	s1 += "++";
	cout << s1 << endl;
}
// pop_back() 删除
void test_string4()
{
	string s1("hello");
	cout << s1 << endl;
	s1.pop_back();
	s1.pop_back();
	cout << s1 << endl; // hel
}
// c_str
void test_string5()
{
	string s1("hello string");
	cout << "调用流插入打印: " << s1 << endl; // 调用重载的流插入运算符打印的;
	cout << "调字符串打印: " << s1.c_str() << endl; // 调用字符串打印，s_str 是遇到斜杠0 '\0'结束的;
}
// push_back和append
// 插入
void test_string()
{
	string s1("hello world");
	s1.push_back('!');
	cout << s1 << endl;
	s1.append("hello bit");
	cout << s1 << endl;
	s1.append(10, 'x');
	cout << s1 << endl;

	string s2(" apple ");
	s1.append(++s2.begin(), --s2.end());
	cout << s1 << endl;
	//s1.append(s2);
	//cout << s1 << endl;
}
// assign、Insert、erase、replace
void test_string6()
{
	string s1("xhello world");
	cout << s1 << endl;
	
	// 为字符串分配新值，并替换当前内容
	s1.assign(" xxxxxx");
	cout << s1 << endl;

	// 头插
	s1.insert(0, "yyyyyy");
	cout << s1 << endl;
	
	// 在第5个位置删除3个字符
	s1.erase(5, 3);
	cout << s1 << endl;
	// 删除所有字符
	//s1.erase();
	//cout << s1 << endl;

	// 删除begin+1到end的字符
	s1.erase(s1.begin() + 1, s1.end());
	cout << s1 << endl;

	// replace 用20%取代从第5个字符开始的第一个字符
	string s2("hello world hello bit");
	s2.replace(5, 1, "20%");
	cout << s2 << endl;
}
// find 查找

void test_string7()
{
	string s1("hello world hello bit");
	cout << s1 << endl;
	size_t pos = s1.find(' ');
	while (pos != string::npos)
	{
		s1.replace(pos, 1, "20%");
		pos = s1.find(' ');
	}
	cout << s1 << endl << endl;
}
// substr 
// 复制 从指定位置开始，一直到结尾
void test_string8()
{
	string s1("file.cpp");
	size_t pos1 = s1.find('.');
	if (pos1 != string::npos)
	{
		string suffix = s1.substr(pos1);
		//string suffix = s1.substr(pos1, s1.size() - pos1);
		cout << suffix << endl;
	}
	else
	{
		cout << "没有后缀" << endl;
	}
}
void test_string8_1()
{
	string s1("file.txt");
	size_t pos = s1.find('.');
	if (pos != string::npos)
	{
		string suffix = s1.substr(pos, s1.size() - pos);
		cout << suffix << endl;
	}
}
// 取网址 URL
void test_string9()
{
	string url1("https://legacy.cplusplus.com/reference/string/string/substr/");
	//string url2("http://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=65081411_1_oem_dg&wd=%E5%90%8E%E7%BC%80%20%E8%8B%B1%E6%96%87&fenlei=256&rsv_pq=0xc17a6c03003ede72&rsv_t=7f6eqaxivkivsW9Zwc41K2mIRleeNXjmiMjOgoAC0UgwLzPyVm%2FtSOeppDv%2F&rqlang=en&rsv_dl=ib&rsv_enter=1&rsv_sug3=4&rsv_sug1=3&rsv_sug7=100&rsv_sug2=0&rsv_btype=i&inputT=1588&rsv_sug4=6786");
	
	// 取协议
	size_t pos1 = url1.find(':'); // 从第0个位置开始找冒号：
	string protocol = url1.substr(0, pos1 - 0); // 从第0个位置开始截取 pos1-0 个字符
	cout << protocol << endl;
	
	// 取域名
	size_t pos2 = url1.find('/', pos1 + 3);// 从pos1+3的位置开始找字符'/'的下标
	string domain = url1.substr(pos1 + 3, pos2 - (pos1 + 3)); // 从 pos1+3 位置开始 截取 pos2-（pos1+3）个字符
	cout << domain << endl;

	// 取资源名
	size_t pos3 = url1.find('/');
	string uri = url1.substr(pos2 + 1, url1.size() - 1);
	cout << uri << endl;
}
void test_string10()
{
	string url1("https://legacy.cplusplus.com/reference/string/string/find/");
	string url2("https://blog.csdn.net/2301_78190552/article/details/139350796");
	//string url1("http://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=65081411_1_oem_dg&wd=%E5%90%8E%E7%BC%80%20%E8%8B%B1%E6%96%87&fenlei=256&rsv_pq=0xc17a6c03003ede72&rsv_t=7f6eqaxivkivsW9Zwc41K2mIRleeNXjmiMjOgoAC0UgwLzPyVm%2FtSOeppDv%2F&rqlang=en&rsv_dl=ib&rsv_enter=1&rsv_sug3=4&rsv_sug1=3&rsv_sug7=100&rsv_sug2=0&rsv_btype=i&inputT=1588&rsv_sug4=6786");

	string protocol, domain, uri;

	size_t i1 = url1.find(':');
	if (i1 != string::npos)
	{
		protocol = url1.substr(0, i1 - 0);
		cout << protocol << endl;
	}

	size_t i2 = url1.find('/', i1 + 3);
	if (i2 != string::npos)
	{
		domain = url1.substr(i1 + 3, i2 - (i1+3));
		cout << domain << endl;
	}
	
	size_t i3 = url1.find('/');
	if (i3 != string::npos)
	{
		uri = url1.substr(i2 + 1);
		cout << uri << endl;
	}
}
int main()
{
	//test_string();
	//test_string1();
	//test_string2();
	//test_string3();
	//test_string4();
	//test_string5();
	//test_string6();
	//test_string7();
	//test_string8();
	//test_string8_1();
	//test_string9();
	test_string10();
	return 0;
}